package com.cadre.common.utils.oldutil;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/* loaded from: FileUtils.class */
public class FileUtils {
    public static void createOutputFile(String filePath) {
        try {
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean isOSlinux() {
        String os = System.getProperties().getProperty("os.name");
        System.out.println("system:" + os);
        if (os == null || os.toLowerCase().indexOf("linux") <= -1) {
            return false;
        }
        return true;
    }

    public static void saveZipFiles(List<String> srcfiles, String zipFilePath, String zipFileName) {
        try {
            File file = new File(zipFilePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            zipFile(srcfiles, new ZipOutputStream(new FileOutputStream(zipFilePath + File.separator + zipFileName)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downloadZipFiles(HttpServletResponse response, List<String> srcFiles, String zipFileName) {
        try {
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("UTF-8"), "ISO8859-1"));
            zipFile(srcFiles, new ZipOutputStream(response.getOutputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void zipFile(List<String> filePaths, ZipOutputStream zos) {
        try {
            byte[] buffer = new byte[4096];
            try {
                for (String filePath : filePaths) {
                    File inputFile = new File(filePath);
                    if (inputFile.exists()) {
                        if (inputFile.isFile()) {
                            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile));
                            zos.putNextEntry(new ZipEntry(inputFile.getName()));
                            while (true) {
                                int size = bis.read(buffer);
                                if (size <= 0) {
                                    break;
                                }
                                zos.write(buffer, 0, size);
                            }
                            zos.closeEntry();
                            bis.close();
                        } else {
                            File[] files = inputFile.listFiles();
                            List<String> filePathsTem = new ArrayList<>();
                            for (File fileTem : files) {
                                filePathsTem.add(fileTem.toString());
                            }
                            zipFile(filePathsTem, zos);
                        }
                    }
                }
                if (null != zos) {
                    try {
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e2) {
                e2.printStackTrace();
                if (null != zos) {
                    try {
                        zos.close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                }
            }
        } catch (Throwable th) {
            if (null != zos) {
                try {
                    zos.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            throw th;
        }
    }

    public static void main(String[] args) {
        System.out.println("isOSLinux:" + isOSlinux());
        System.out.println("roots:" + File.listRoots()[1]);
    }
}
